package SingleMachineArrangeImpl;

import java.util.Arrays;

//最早开工时间优先
public class EST extends SingleMachineArrange{
    @Override
    public void solve() {
        if (process == null || ready == null) {
            process = new int[]{3, 18, 17, 21, 25};
            ready = new int[]{35, 22, 34, 37, 66};
        }
        int[] complete = new int[process.length];
        int[] s = new int[process.length];
        final int COMPLETE = -1;
        int t = 0;
        int n = process.length;

        for (int i = 0; i < n; i++) {

            int s_ = Integer.MAX_VALUE;
            int s_j = -1;
            for (int j = 0; j < n; j++) {
                //用0表示已安排
                if (process[j] == COMPLETE) continue;
                s[j] = Math.max(ready[j], t);
                complete[j] = s[j] + process[j];
                //取得最早开工工件的下标
                if (s[j] < s_) {
                    s_ = s[j];
                    s_j = j;
                }
            }
            t = complete[s_j];
            process[s_j] = COMPLETE;
            System.out.println("proc:" + Arrays.toString(process));
            System.out.println("comp:" + Arrays.toString(complete));
            System.out.println("comp_j = " + (s_j + 1));
        }
        System.out.println(t);
    }
}
